/*
 * Copyright (c) 2020 hebeiyiliuba.com
 * All rights reserved.
 *
 */
package org.jeecg.modules.middleware.redis.message.service;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.middleware.redis.message.constant.enums.RedisChannelEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Redis消息推送-订阅者
 * @author duanlikao
 * @created 2021/8/13
 */
@Service
public class PublisherRedisMessageService {
    @Autowired
    private RedisUtil redisUtil;

    /**
     *  Redis消息按topic推送
     * @return
     */
    public boolean publisherRedisMessage(RedisChannelEnum channelEnum, Object obj){
        if(StringUtils.isBlank(channelEnum.getChannel())){
            return false;
        }
        if(obj == null){
            return false;
        }
        return  redisUtil.convertAndSend(channelEnum.getChannel(),JSONObject.toJSONString(obj));
    }

    public boolean publisherRedisMessage(RedisChannelEnum channelEnum,String msg){
        if(StringUtils.isBlank(msg)){
            return false;
        }
        if(StringUtils.isBlank(channelEnum.getChannel())){
            return false;
        }
        return  redisUtil.convertAndSend(channelEnum.getChannel(),msg);
    }
}
